/**
 * @Copyright © 2015 广州四三九九信息科技有限公司. All Rights Reserved.
*/

package com.jkdroid.maya.log;

import android.content.Context;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 日志策略抽象类
 * @author guanzhihao
 * @date   2015年11月2日
 * 
 * @param <Item>
 */
public abstract class AbstractEventLogStrategy<Item> {

	private static final int DEFAULT_MAX_RECORD_SIZE = 500;
	private static final int DEFAULT_AUTO_NUM = 10;
	
	private int mMaxRecordSize;
	private final AtomicBoolean mEnableAuto;
	private int mAutoNum;
	
	public AbstractEventLogStrategy(){
		mEnableAuto = new AtomicBoolean(false);
		mMaxRecordSize = DEFAULT_MAX_RECORD_SIZE;
		mAutoNum = DEFAULT_AUTO_NUM;
	}
	
	public final AbstractEventLogStrategy<Item> setMaxRecordSize(int size){
		this.mMaxRecordSize = size;
		return this;
	}
	
	public final AbstractEventLogStrategy<Item> enableAutoSend(boolean enable){
		mEnableAuto.set(enable);
		return this;
	}
	
	public final AbstractEventLogStrategy<Item> setAutoSendNum(int num){
		this.mAutoNum = num;
		return this;
	}
	
	protected final int getMaxRecordSize(){
		return mMaxRecordSize;
	}
	
	protected final boolean isEnableAutoSend(){
		return this.mEnableAuto.get();
	}
	
	protected final int getAutoSendNum(){
		return this.mAutoNum;
	}

	public void clearAll(Context context){
		clearEventLogs(context);
		clearBackupEventLogs(context);
	}
	
	/**
	 * 获取记录的日志
	 * @param context
	 * @return
	 */
	protected abstract ArrayList<Item> getEventLogs(Context context);

	/**
	 * 创建日志
	 * @param eventId
	 * @param eventName
	 * @param eventValue
	 * @return
	 */
	protected abstract Item createEvent(String eventId, String eventName, String eventValue);

	/**
	 * 发送日志
	 * @param context
	 * @param records
	 * @return
	 */
	protected abstract boolean sendEventLogs(Context context, ArrayList<Item> records);

	/**
	 * 发送前备份日志
	 * @param context
	 */
	protected abstract void backupEventlogs(Context context);

	/**
	 * 获取备份的日志
	 * @param context
	 * @return
	 */
	protected abstract ArrayList<Item> getBackupEventLogs(Context context);

	/**
	 * 保存日志
	 * @param context
	 * @param records
	 */
	public abstract void saveEventLogs(Context context, ArrayList<Item> records);
	
	/**
	 * 清除日志
	 * @param context
	 */
	protected abstract void clearEventLogs(Context context);
	
	/**
	 * 清除备份
	 * @param context
	 */
	protected abstract void clearBackupEventLogs(Context context);

}
